package com.qst.financing.controller;

import com.github.pagehelper.PageInfo;
import com.qst.financing.common.Result;
import com.qst.financing.common.StatusCode;
import com.qst.financing.entity.Finance;
import com.qst.financing.entity.Recommender;
import com.qst.financing.service.FinanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

/**
 * @author qst
 * @Description 融资模块模块
 */
@RestController
@RequestMapping("/finance")
@Tag(name = "融资模块接口")
public class BankFinanceController {
    @Autowired
    private FinanceService financeService;

    @Value("${bank.bankid}")
    private Integer bankid;

    //查询推荐融资人
    @Operation(summary = "分页查询推荐融资人")
    @GetMapping("/selectRecommend/{pageNum}")
    public Result selectRecommend(@PathVariable("pageNum") Integer pageNum) {
        PageInfo<Recommender> list = financeService.selectRecommend(pageNum);
        return new Result(true, StatusCode.OK, "查询成功",list);
    }
    //根据银行id查询银行名称
    @GetMapping("/selectBankName")
    public Result selectBankName() {
        String bankName = financeService.selectBankName(bankid);
        return new Result(true, StatusCode.OK, "查询成功",bankName);
    }

    //根据id修改融资情报
    @Operation(summary = "根据id修改融资情报")
    @PutMapping("/update")
    public Result update(@RequestBody Finance finance) {
        financeService.updateByFinance(finance);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //根据id查询融资情报
    @Operation(summary = "根据id查询融资情报")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Finance finance = financeService.selectByFinanceId(id);
        return new Result(true, StatusCode.OK, "查询成功", finance);
    }

    //根据银行id与申请人查询融资情报
    @Operation(summary = "根据银行id与申请人查询融资情报")
    @GetMapping("/selectByFinance/{keys}/{pageNum}")
        public Result<PageInfo> selectByFinance(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {

        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = financeService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }

    //根据银行id查询融资情报
    @Operation(summary = "根据银行id查询融资情报")
    @GetMapping("/selectByBankId/{pageNum}")
    public Result<PageInfo> selectByBankId(@PathVariable("pageNum") Integer pageNum) {

        Finance financeKey = new Finance();
        financeKey.setBankId(bankid);
        PageInfo<Finance> financePageInfo = financeService.selectByFinance(financeKey,pageNum);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", financePageInfo);
    }
}
